home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / kitten / RCS / kitten.c,v < prev   
Text File  |  1992-07-16  |  4KB  |  237 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  srv030:1.3 srv027:1.3 srv026:1.3 srv024:1.3 srv021:1.3 srv018:1.3 srv014:1.3 srv010:1.3 srv008:1.3 srv007:1.3 srv006:1.3 srv004:1.3;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     92.03.23.15.10.10;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     91.12.12.22.36.52;  author kupfer;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     91.12.01.22.43.56;  author kupfer;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @Small "cat".
  27. @
  28.  
  29.  
  30. 1.3
  31. log
  32. @Add support for /dev/console and optionally shutting down rather than
  33. just exiting.
  34. @
  35. text
  36. @/* small "cat" program. */
  37.  
  38. #ifndef lint
  39. static char rcsid[] = "$Header: /user5/kupfer/spriteserver/tests/kitten/RCS/kitten.c,v 1.2 91/12/12 22:36:52 kupfer Exp Locker: kupfer $";
  40. #endif
  41.  
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <sys.h>
  45. #include <sys/fcntl.h>
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48. #include <test.h>
  49. #include <unistd.h>
  50.  
  51. #define USE_MALLOC    1
  52. #define BUF_SIZE    8192
  53. #define USE_CONSOLE    1    /* 0 to use Test_foo to write each line */
  54. #define DO_SHUTDOWN    0    /* 0 to just exit quietly */
  55.  
  56. static void CatFile();
  57. static void DoFile();
  58.  
  59. int
  60. main(argc, argv)
  61.     int argc;
  62.     char *argv[];
  63. {
  64.     int i;
  65.  
  66.     for (i = 1; i < argc; i++) {
  67.     if (argv[i][0] == '-') {
  68.         continue;
  69.     }
  70.     DoFile(argv[i]);
  71.     }
  72.  
  73. #if DO_SHUTDOWN
  74.     (void)Sys_Shutdown(SYS_HALT|SYS_KILL_PROCESSES|SYS_WRITE_BACK, NULL);
  75. #endif
  76.     return 0;
  77. }
  78.  
  79.  
  80. /*
  81.  *----------------------------------------------------------------------
  82.  *
  83.  * DoFile --
  84.  *
  85.  *    Process the named file.
  86.  *
  87.  * Results:
  88.  *    None.
  89.  *
  90.  * Side effects:
  91.  *    If it's a text file, its contents are printed.  For anything else, 
  92.  *    stat information is given for it.
  93.  *
  94.  *----------------------------------------------------------------------
  95.  */
  96.  
  97. static void
  98. DoFile(fileName)
  99.     char *fileName;
  100. {
  101.     struct stat statBuf;
  102.  
  103.     if (stat(fileName, &statBuf) < 0) {
  104.     perror(fileName);
  105.     return;
  106.     }
  107.  
  108.     if ((statBuf.st_mode & S_IFMT) == S_IFREG) {
  109.     CatFile(fileName);
  110.     } else {
  111.     Test_PutMessage("Type: ");
  112.     Test_PutOctal(statBuf.st_mode & S_IFMT);
  113.     Test_PutMessage("\nServer: ");
  114.     Test_PutDecimal(statBuf.st_serverID);
  115.     Test_PutMessage("\nDevice: ");
  116.     Test_PutDecimal(statBuf.st_dev);
  117.     Test_PutMessage("\nI-number: ");
  118.     Test_PutDecimal(statBuf.st_ino);
  119.     Test_PutMessage("\nOwner: ");
  120.     Test_PutDecimal(statBuf.st_uid);
  121.     Test_PutMessage("\n");
  122.     }
  123. }
  124.  
  125.  
  126. /*
  127.  *----------------------------------------------------------------------
  128.  *
  129.  * CatFile --
  130.  *
  131.  *    Print the contents of a file.
  132.  *
  133.  * Results:
  134.  *    None.
  135.  *
  136.  * Side effects:
  137.  *    None.
  138.  *
  139.  *----------------------------------------------------------------------
  140.  */
  141.  
  142. static void
  143. CatFile(fileName)
  144.     char *fileName;
  145. {
  146. #ifdef USE_MALLOC
  147.     char *buf;
  148. #else
  149.     char buf[BUF_SIZE];
  150. #endif
  151.     int file;            /* file descriptor */
  152.     int nChars;            /* number of characters read */
  153. #if USE_CONSOLE
  154.     int cons;            /* console file descriptor */
  155. #endif
  156.  
  157. #ifdef USE_MALLOC
  158.     buf = malloc(BUF_SIZE);
  159.     if (buf == NULL) {
  160.     Test_PutMessage("malloc failed.\n");
  161.     exit(1);
  162.     }
  163. #endif
  164.     file = open(fileName, O_RDONLY);
  165.     if (file < 0) {
  166.     perror(fileName);
  167.     exit(1);
  168.     }
  169. #if USE_CONSOLE
  170.     cons = open("/dev/console", O_WRONLY, 0666);
  171.     if (cons < 0) {
  172.     perror("can't open console");
  173.     exit(1);
  174.     }
  175. #endif /* USE_CONSOLE */
  176.  
  177.     while ((nChars = read(file, buf, BUF_SIZE)) > 0) {
  178. #if USE_CONSOLE
  179.     if (write(cons, buf, nChars) < 0) {
  180.         perror("can't write to console");
  181.         exit(1);
  182.     }
  183. #else
  184.     Test_PutString(buf, nChars);
  185. #endif
  186.     }
  187.     if (nChars < 0) {
  188.     perror("read");
  189.     exit(1);
  190.     }
  191.  
  192.     close(file);
  193. #ifdef USE_MALLOC
  194.     free(buf);
  195. #endif
  196. }
  197.  
  198. @
  199.  
  200.  
  201. 1.2
  202. log
  203. @Let buffer either be malloc'd or allocated on the stack.
  204. @
  205. text
  206. @d4 1
  207. a4 1
  208. static char rcsid[] = "$Header$";
  209. d9 1
  210. d18 2
  211. d38 3
  212. d70 1
  213. a70 1
  214.     exit(1);
  215. d118 3
  216. d131 7
  217. a137 1
  218.     perror("open");
  219. d140 1
  220. d143 6
  221. d150 1
  222. @
  223.  
  224.  
  225. 1.1
  226. log
  227. @Initial revision
  228. @
  229. text
  230. @d3 4
  231. d15 1
  232. d105 3
  233. d109 1
  234. d113 7
  235. d135 3
  236. @
  237.